package com.jz.netthings.application.providerReg.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.netthings.appengine.baseData.JzMachine;
import com.jz.netthings.appengine.baseData.ProviderInfo;
import com.jz.netthings.appengine.mapper.JzMachineMapper;
import com.jz.netthings.appengine.mapper.ProviderInfoMapper;
import com.jz.netthings.application.providerReg.bean.ProviderReg;
import com.jz.netthings.application.providerReg.bean.QueryProviderReg;
import com.jz.netthings.application.providerReg.mapper.mysql.ProviderRegMapper;
import com.jz.netthings.application.providerReg.service.ProviderRegService;
import com.jz.netthings.bean.system.JyCode;
import com.jz.netthings.bean.system.JyUser;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.system.mapper.mysql.JyCodeMapper;
import com.jz.netthings.system.mapper.mysql.JyUserMapper;
import com.jz.netthings.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-02-28
 */
@Service
public class ProviderRegServiceImpl extends ServiceImpl<ProviderRegMapper, ProviderReg> implements ProviderRegService {
     @Autowired
     ProviderInfoMapper providerInfoMapper;
     @Autowired
     JyUserMapper jyUserMapper;
     @Autowired
     JzMachineMapper jzMachineMapper;
     @Autowired
    JyCodeMapper jyCodeMapper;
     @Autowired
     ProviderRegMapper providerRegMapper;
     @Value("${ly.upload.baseUrl}")
      String photoUrl;

    @Override
    public Page<ProviderReg> selectByTime(Page<ProviderReg> page, QueryProviderReg queryProviderReg) throws AdscmSystemException {
        Wrapper<ProviderReg> wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(  queryProviderReg.getCardId(),null) &&AdscmBaseUtils.getInstance().isNotEmpty(  queryProviderReg.getProviderName(),null) ){
                Wrapper<ProviderInfo> providerInfoWrapper =new EntityWrapper<>();
                providerInfoWrapper.eq("cardId",queryProviderReg.getCardId());
                providerInfoWrapper.eq("name",queryProviderReg.getProviderName().trim());
            List<ProviderInfo> providerInfos = providerInfoMapper.selectList(providerInfoWrapper);
            if(providerInfos.size()>0){
                ProviderInfo providerInfo = providerInfos.get(0);
                wrapper.eq("providerNo",providerInfo.getId());
            }else {
                throw new AdscmSystemException("查无此人");
            }
        }
        if (!AdscmBaseUtils.getInstance().isNotEmpty(queryProviderReg.getCardId(),null) && AdscmBaseUtils.getInstance().isNotEmpty(  queryProviderReg.getProviderName(),null)){
            Wrapper<ProviderInfo> providerInfoWrapper =new EntityWrapper<>();
            providerInfoWrapper.eq("name",queryProviderReg.getProviderName().trim());
            List<ProviderInfo> providerInfos = providerInfoMapper.selectList(providerInfoWrapper);
            if (providerInfos.size()>0){
                for (int i = 0; i < providerInfos.size(); i++) {
                    if (i==0){
                        wrapper.eq("providerNo",providerInfos.get(i).getId());
                    }else {
                        wrapper.orNew();
                        wrapper.eq("providerNo",providerInfos.get(i).getId());
                    }

                }
            }else {
                throw new AdscmSystemException("查无此人");
            }
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(  queryProviderReg.getCardId(),null) && !AdscmBaseUtils.getInstance().isNotEmpty(  queryProviderReg.getProviderName(),null)) {
            Wrapper<ProviderInfo> providerInfoWrapper =new EntityWrapper<>();
            providerInfoWrapper.eq("cardId",queryProviderReg.getCardId());
            List<ProviderInfo> providerInfos = providerInfoMapper.selectList(providerInfoWrapper);
            if(providerInfos.size()>0){
                ProviderInfo providerInfo = providerInfos.get(0);
                wrapper.eq("providerNo",providerInfo.getId()+"");
            }else {
                throw new AdscmSystemException("查无此人");
            }
        }
            if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderReg.getStartTime(),null) ){
            wrapper.ge("regTime",queryProviderReg.getStartTime());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderReg.getEndTime(),null) ){
            wrapper.le("regTime",queryProviderReg.getEndTime());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderReg.getIdNo(),null)){
            wrapper.eq("idNo",queryProviderReg.getIdNo());
        }
         if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderReg.getMacCode(),null)){
             wrapper.eq("machineCode",queryProviderReg.getMacCode());
         }



        wrapper.setSqlSelect("providerNo","regTime","idNo","registerNo","registerIdx","rzfs","regNurse","regPhoto","machineCode","thresholdValue","createTime");
        page = super.selectPage(page, wrapper);

//        List<ProviderReg> regsList = providerRegMapper.selectPage(page,wrapper);
//        page.setRecords(regsList);

        List<ProviderReg> providerRegs = page.getRecords();
        for (ProviderReg providerReg : providerRegs) {
            // 查询基础信息在登记返回的数据中
              if (AdscmBaseUtils.getInstance().isNotEmpty(providerReg.getProviderNo(),null)){
                  ProviderInfo providerInfo = providerInfoMapper.selectById(providerReg.getProviderNo());
                  if (AdscmBaseUtils.getInstance().isNotEmpty(providerInfo,null)){
                      providerReg.setProviderNoName(providerInfo.getName());
                      providerReg.setCardId(providerInfo.getCardId());
                      providerReg.setCreatePhoto(photoUrl+providerInfo.getPhoto());
                      providerReg.setRegPhoto(photoUrl+providerReg.getRegPhoto());
                      providerReg.setAge(providerInfo.getAge().toString());
                      if(AdscmBaseUtils.getInstance().isNotEmpty( providerInfo.getSex(),null)){
                          JyCode  jyCode =new JyCode();
                          jyCode.setType(7);
                          jyCode.setCode(providerInfo.getSex());
                          jyCode= jyCodeMapper.selectOne(jyCode);
                          if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                              providerReg.setSex(jyCode.getName());
                          }else {
                              providerReg.setSex(providerInfo.getSex());
                          }
                      }
                  }else {
                      providerReg.setProviderNoName(providerReg.getProviderNo());
                  }
              }
              if (AdscmBaseUtils.getInstance().isNotEmpty(providerReg.getRegNurse(),null)){
                  JyUser jyUser = jyUserMapper.selectById(providerReg.getRegNurse());
                  if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                      providerReg.setRegNurseName(jyUser.getName());
                  }else {
                      providerReg.setRegNurseName(providerReg.getRegNurse());
                  }
              }
              if (AdscmBaseUtils.getInstance().isNotEmpty(providerReg.getMachineCode(),null)){
                  JzMachine jzMachine = jzMachineMapper.selectById(providerReg.getMachineCode());
                   if (AdscmBaseUtils.getInstance().isNotEmpty(jzMachine,null)){
                       providerReg.setMachineCodeName(jzMachine.getName());
                   }else {
                       providerReg.setMachineCodeName(providerReg.getMachineCode());
                   }
              }
              if (AdscmBaseUtils.getInstance().isNotEmpty(providerReg.getRzfs(),null)){
                      JyCode  jyCode =new JyCode();
                      jyCode.setType(6);
                      jyCode.setCode(providerReg.getRzfs());
                      jyCode= jyCodeMapper.selectOne(jyCode);
                      if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                          providerReg.setRzfsName(jyCode.getName());
                      }else {
                          providerReg.setRzfsName(providerReg.getRzfs());
                      }
              }


        }
        page.setTotal(super.selectCount(wrapper));
        return page;
    }

    @Override
    public ProviderReg selectProviderReg(String registerNo) throws AdscmSystemException {
        ProviderReg  providerReg =new ProviderReg();
           Wrapper<ProviderReg> wrapper =new EntityWrapper<>();
           wrapper.eq("registerNo",registerNo);
           List<ProviderReg> providerRegs = super.selectList(wrapper);
            if (providerRegs.size()>0){
                providerReg=   providerRegs.get(0);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerReg.getProviderNo(),null)){
                ProviderInfo providerInfo = providerInfoMapper.selectById(providerReg.getProviderNo());
                if (AdscmBaseUtils.getInstance().isNotEmpty(providerInfo,null)){
                    providerReg.setProviderNoName(providerInfo.getName());
                    providerReg.setCardId(providerInfo.getCardId());
                    providerReg.setCreatePhoto(photoUrl+providerInfo.getPhoto());
                    providerReg.setRegPhoto(photoUrl+providerReg.getRegPhoto());
                    providerReg.setAge(providerInfo.getAge().toString());
                    if(AdscmBaseUtils.getInstance().isNotEmpty( providerInfo.getSex(),null)){
                        JyCode  jyCode =new JyCode();
                        jyCode.setType(7);
                        jyCode.setCode(providerInfo.getSex());
                        jyCode= jyCodeMapper.selectOne(jyCode);
                        if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                            providerReg.setSex(jyCode.getName());
                        }else {
                            providerReg.setSex(providerInfo.getSex());
                        }
                    }
                }else {
                    providerReg.setProviderNoName(providerReg.getProviderNo());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerReg.getRegNurse(),null)){
                JyUser jyUser = jyUserMapper.selectById(providerReg.getRegNurse());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    providerReg.setRegNurseName(jyUser.getName());
                }else {
                    providerReg.setRegNurseName(providerReg.getRegNurse());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerReg.getMachineCode(),null)){
                JzMachine jzMachine = jzMachineMapper.selectById(providerReg.getMachineCode());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jzMachine,null)){
                    providerReg.setMachineCodeName(jzMachine.getName());
                }else {
                    providerReg.setMachineCodeName(providerReg.getMachineCode());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerReg.getRzfs(),null)){
                JyCode  jyCode =new JyCode();
                jyCode.setType(6);
                jyCode.setCode(providerReg.getRzfs());
                jyCode= jyCodeMapper.selectOne(jyCode);
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                    providerReg.setRzfsName(jyCode.getName());
                }else {
                    providerReg.setRzfsName(providerReg.getRzfs());
                }
            }
        return  providerReg;
    }
    

}
